package com.hschinese.pronunciation.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.bean.TestResultBean;
import com.hschinese.pronunciation.utils.Utils;
import com.hschinese.pronunciation.widget.TextProgressBar;

import java.util.List;

/**
 * Created by wo on 2015/11/3.
 */
public class ToneResultAdapter extends BaseAdapter {
    private Context mContext = null;
    private List<TestResultBean> mData = null;
    private LayoutInflater inflater;

    public ToneResultAdapter(List<TestResultBean> data, Context cxt) {
        mData = data;
        mContext = cxt;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.tone_result_item, parent, false);
            holder = new ViewHolder();
            holder.toneTitle = (TextView)convertView.findViewById(R.id.tone_title);
            holder.tv_zhengquelv = (TextView)convertView.findViewById(R.id.tv_zhengquelv);
            holder.toneImg = (ImageView)convertView.findViewById(R.id.tone_icon);
            holder.mProgress = (TextProgressBar)convertView.findViewById(R.id.tone_progress);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        // TODO 填充图片，将图片
        TestResultBean data = mData.get(position);
        // TODO 数据填充
        if (position == 0) {
            holder.toneTitle.setText("a,i,u");
            holder.toneTitle.setTextColor(Color.WHITE);
            holder.toneTitle.setVisibility(View.VISIBLE);

        } else if (position == 4) {
            holder.toneTitle.setText("aa");
            holder.toneTitle.setVisibility(View.VISIBLE);
            holder.toneTitle.setTextColor(Color.WHITE);
        } else {
            holder.toneTitle.setVisibility(View.GONE);
        }
        holder.toneImg.setImageResource(Utils.getResId(mContext, "ic_" + data.getTone().replace("|", ""), "drawable"));
//        Logger.e("xionghy - " + data.getRightTimes() + " @ " + data.getWrongTimes());
        int total = data.getRightTimes() + data.getWrongTimes();
        int rightTimes = data.getRightTimes();
        holder.mProgress.setMax(total);
        holder.mProgress.setProgress(rightTimes);
        if(data.getRightTimes() + data.getWrongTimes()==0){
            holder.tv_zhengquelv.setText("0%");
        }else {
           String str= (rightTimes*1.0/total)*100+"%";

            holder.tv_zhengquelv.setText(str);
        }

        return convertView;
    }

    class ViewHolder {
        public TextView toneTitle;
        public TextView tv_zhengquelv;
        public ImageView toneImg;
        public TextProgressBar mProgress;
    }

    public static int getResId(Context cxt, String name, String defType) {
        String packageName = cxt.getApplicationInfo().packageName;
        Resources mResources = cxt.getResources();
        return mResources.getIdentifier(name, defType, packageName);
    }

}
